﻿using System.Collections.Generic;
using Lu.Blog.Domain.Model.DocumentType;
using Lu.Blog.Domain.Model.Menu;
using umbraco.interfaces;
using Vega.USiteBuilder;

namespace Lu.Blog.Domain.Provider.Impl
{
    public class MenuProviderImpl : IMenuProvider
    {
        public IEnumerable<MenuItem> GetMenu(INode node, int depth = 1)
        {
            if (depth <= 0) yield break;

            foreach (INode child in node.ChildrenAsList)
            {
                if (child.GetProperty("hideInNavigation").Value == "1")
                {
                    continue;
                }
                var currentPage = ContentHelper.GetByNodeId<BaseNavigation>(child.Id);
                yield return new MenuItem
                {
                    Title =
                        (string.IsNullOrEmpty(currentPage.NavigationTitle)
                            ? currentPage.Name
                            : currentPage.NavigationTitle),
                    Children = GetMenu(node, depth - 1),
                    CurrentDocument = currentPage,
                    Url = currentPage.NiceUrl
                };
            }
        }

        public IEnumerable<MenuItem> GetMainMenu(INode node, int depth = 1)
        {
            DocumentTypeBase thisPage = ContentHelper.GetByNodeId(node.Id);
            var homepage = thisPage.TypedAncestorOrSelf<HomePage>();

            var retList = new List<MenuItem>
            {
                new MenuItem
                {
                    Children = GetMenu(homepage, depth - 1),
                    CurrentDocument = homepage,
                    Title = (string.IsNullOrEmpty(homepage.NavigationTitle) ? homepage.Name : homepage.NavigationTitle),
                    Url = homepage.NiceUrl
                }
            };
            retList.AddRange(GetMenu(homepage, depth));

            return retList;
        }
    }
}